﻿using System;
using System.Collections.Generic;
using System.Text;
using MyIni;

namespace CadStrategy2013.CgqStyle
{
    public class TongJiClass
    {

        //string[] cgqBianHaos;
        //int cgqBianHaoIndex;
        Dictionary<string, CgqNoStatistics> cgqBianHaosDic = new Dictionary<string, CgqNoStatistics>();

        string[] m_portNames;

        int m_count;

        public int Count
        {
            get { return m_count; }
            set { m_count = value; }
        }
        string m_TongXingJiPort;

        public string TongXingJiPort
        {
            get { return m_TongXingJiPort; }
            set { m_TongXingJiPort = value; }
        }
        int m_CgqNo;

        public int CgqNo
        {
            get { return m_CgqNo; }
            set { m_CgqNo = value; }
        }

        public TongJiClass()
        {

        }

        public TongJiClass(string filename)
        {
            List<string> list = new List<string>();
            IniFile inifile = new IniFile(filename);
            inifile.Load();
            string[] secs = inifile.GetSectionNames();
            foreach(string str in secs)
            {
                string s = inifile.GetString(str, "编号");
                if (!string.IsNullOrEmpty(s))
                {
                    CgqNoStatistics c = new CgqNoStatistics();
                    c.BianHaos = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    c.Index = 0;
                    cgqBianHaosDic.Add(str, c);
                    foreach (string s2 in c.BianHaos)  //得到通信机端口号集合
                    {
                        string[] s3 = s2.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                        if (!list.Contains(s3[0]))
                        {
                            list.Add(s3[0]);
                        }
                    }
                }
            }
            m_portNames = list.ToArray();
        }
        /// <summary>
        /// 得到dyhx.ini中 智能传感器编号=“”中对应的字符串
        /// </summary>
        /// <param name="cgqStyleName"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public string[]  GetCgqBianHao(string cgqStyleName, int count)
        {
            string[] strs =new string[2];
            strs[0] = "";
            strs[1] = GetJkbBianHao(cgqStyleName,count);

            if(cgqBianHaosDic.ContainsKey(cgqStyleName))
            {
                CgqNoStatistics c = cgqBianHaosDic[cgqStyleName];
                for (int i = 0; i <count ; i++)
                {
                    if (i + c.Index > c.BianHaos.Length) break;
                    strs[0] = strs[0]+c.BianHaos[i + c.Index]+",1,"+cgqStyleName+";";
                    
                }
                c.Index = count - 1;
            }

            return strs;
        }

        /// <summary>
        /// 得到dyhx.ini中 接口板编号=“”中对应的字符串
        /// </summary>
        /// <param name="cgqStyleName"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        private string GetJkbBianHao(string cgqStyleName, int count)
        {
            string str = "";
            string strn = "";
            switch (cgqStyleName)
            {
                case "半自动":
                case "直流道岔电流":
                    strn = "4";
                    break;
                case "站内电码化":
                case "交流道岔电流":
                case "无绝缘发送":
                    strn = "2";
                    break;
                default:
                    return "";
            }
            if (cgqBianHaosDic.ContainsKey(cgqStyleName))
            {
                CgqNoStatistics c = cgqBianHaosDic[cgqStyleName];
                for (int i = 0; i < count; i++)
                {
                    if (i + c.Index > c.BianHaos.Length) break;
                    str =str+ c.BianHaos[i + c.Index] + "JK-1," + strn + ",1;";

                }
                return str;
            }

            return "";
        }

        /// <summary>
        /// 得到datahx.ini中智能传感器编号= 对应的字符串
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        public string GetPortCgqBianHao(string port)
        {
            string strResule="";
            foreach(KeyValuePair<string,CgqNoStatistics> pair in cgqBianHaosDic)
            {
                foreach (string str in pair.Value.BianHaos)
                {
                    string[] strs = str.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
                    if (strs.Length > 1 && (strs[0] == port))
                    {
                        strResule = strResule + str + ",1,"+pair.Key+ ";";
                    }
                }
            }
            return strResule;
        }

        public string[] GetPortNames()
        {
            return m_portNames;
        }

        public int GetMaxPortNo()
        {
            int a=0;
            foreach (string str in m_portNames)
            {
                if (str == "I")
                    a = 1;
            }
            return 1;
        }
    }

    public class CgqNoStatistics
    {
        string[] bianHaos;

        public string[] BianHaos
        {
            get { return bianHaos; }
            set { bianHaos = value; }
        }
        int index;

        public int Index
        {
            get { return index; }
            set { index = value; }
        }
    }
}
